package com.qifan.energy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.qifan.energy.base.BaseActivity;
import com.qifan.energy.bean.result.MemberBalanceResult;
import com.qifan.energy.bean.vo.BalanceWithdrawalVO;
import com.qifan.energy.R;
import com.qifan.energy.bean.result.MemberInfo;
import com.qifan.energy.bean.result.MemberInfoResult;
import com.qifan.energy.global.Keys;
import com.qifan.energy.global.Values;
import com.qifan.energy.http.net.NetUtils;
import com.qifan.energy.http.response.MyObserver;
import com.qifan.energy.util.IntentUtils;
import com.qifan.energy.util.SharedPreferencesUtil;
import com.qifan.energy.util.ToastUtils;
import com.qifan.energy.util.UIUtils;
import com.qifan.energy.widget.dialog.LoadingUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class BalanceActivity extends BaseActivity {

    @BindView(R.id.balance_tv)
    TextView balanceTv;//余额
    @BindView(R.id.withdrawing_tv)
    TextView withdrawingTv;//提现中
    @BindView(R.id.withdrawed_tv)
    TextView withdrawedTv;//累计已提现

    @BindView(R.id.alipay_acount_tv)
    TextView alipayAcountTv;
    @BindView(R.id.amount_et)
    EditText amountEt;
    private MemberInfo memberInfo;
    private MemberInfoResult InfoResult;
    private double totalBalanceAmount = 0.00f;

    /*@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
    }*/

    @Override
    protected int getLayoutId() {
        return R.layout.activity_balance;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        /* 更改状态栏 */
        changeStatusTransparentFontBlack();

        memberInfo = SharedPreferencesUtil.getData(Keys.USER_INFO_MEMBER_KEY,new MemberInfo());
        InfoResult = SharedPreferencesUtil.getData(Keys.USER_INFO_KEY,new MemberInfoResult());
        alipayAcountTv.setText("未绑定");
        alipayAcountTv.setTextColor(UIUtils.getColor(R.color.text_999));
        /* 获取本地用户信息 */
        setUpUI();
    }

    @Override
    protected void initData() {
        memberBalanceBalanceResult();
    }

    //获取余额信息
    private void memberBalanceBalanceResult(){
        NetUtils.memberBalanceBalanceResult(new MyObserver<MemberBalanceResult>(this) {
            @Override
            public void onSuccess(MemberBalanceResult result) {
                //ui赋值
                if (result != null) {
                    totalBalanceAmount =  result.getBalanceAmount();
                    balanceTv.setText(String.valueOf(totalBalanceAmount));
                    withdrawingTv.setText(String.valueOf(result.getWithdrawalBalanceAmount()));
                    withdrawedTv.setText(String.valueOf(result.getAllWithdrawalBalanceAmount()));
                    if (!TextUtils.isEmpty(result.getMember().getAlipayAccount()))  {
                        memberInfo.setAlipayAccount(result.getMember().getAlipayAccount());
                        setUpUI();
                    }
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                ToastUtils.showShort(errorMsg);
            }
        });
    }

    @OnClick(R.id.binding_alipay_ll)
    void onBindingAlipayLlClicked(){
        String alipayAccount = memberInfo.getAlipayAccount();
        if (!TextUtils.isEmpty(alipayAccount)) {//已经绑定 去修改
            Intent intent = new Intent();
            intent.putExtra(Keys.BINDING_ALIPAY_TYPE, 1);
            IntentUtils.startActivityForResult(this,EditAlipayActivity.class,intent, Values.REQUEST_CODE_1);
        } else {
            IntentUtils.startActivityForResult(this,EditAlipayActivity.class,Values.REQUEST_CODE_1);
        }
    }

    @OnClick(R.id.withdraw_all_tv)
    void onwithdrawAllTvClicked(){
        amountEt.setText(String.valueOf(totalBalanceAmount));
        amountEt.setSelection(amountEt.getText().length());
    }

    @OnClick(R.id.done_btn)
    void onDoneBtnClicked(){
        if (TextUtils.isEmpty(memberInfo.getAlipayAccount())) {
            ToastUtils.showShort("您还未绑定支付宝账号哦~");
            return;
        }
        String amount = amountEt.getText().toString().trim();
        if (TextUtils.isEmpty(amount)) {
            ToastUtils.showShort("金额必须填写哦~");
            return;
        } else {
            double amoutD = Double.parseDouble(amount);
            if (amoutD < 10.00){
                ToastUtils.showShort("最低提现金额10元哦~");
                return;
            }

            if (totalBalanceAmount < amoutD) {
                ToastUtils.showShort("您输入的金额超额了哦~");
                return;
            }
            LoadingUtils.show(this);
            BalanceWithdrawalVO balanceWithdrawalVO = new BalanceWithdrawalVO();
            balanceWithdrawalVO.setChangeAmount(amoutD);
            balanceWithdrawalVO.setAlipayAccount(memberInfo.getAlipayAccount());
            NetUtils.memberBalanceWithdrawalAdd(balanceWithdrawalVO, new MyObserver<String>(this) {
                @Override
                public void onSuccess(String result) {
                    LoadingUtils.hide();
                    //提示并且刷新
                    ToastUtils.showShort("已经申请提现");
                    memberBalanceBalanceResult();
                    amountEt.setText("");
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {
                    LoadingUtils.hide();
                    ToastUtils.showShort(errorMsg);
                }
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            memberBalanceBalanceResult();
        }
    }

    //ui赋值
    private void setUpUI() {
        String alipayAccount = memberInfo.getAlipayAccount();
        if (!TextUtils.isEmpty(alipayAccount)) {
            alipayAcountTv.setText(alipayAccount);
            alipayAcountTv.setTextColor(UIUtils.getColor(R.color.black));
        } else {
            alipayAcountTv.setText("未绑定");
            alipayAcountTv.setTextColor(UIUtils.getColor(R.color.text_999));
        }
    }
}
